<template>
  <div>
    <el-card class="box-card">
      <h2>角色列表</h2>
      <el-input
        v-model="searchName"
        placeholder="请输入角色名称"
        style="margin-bottom: 20px;"
      ></el-input>
      <el-button type="primary" @click="searchRole">查询</el-button>
      <el-table :data="roles" style="width: 100%; margin-top: 20px;">
        <el-table-column prop="name" label="角色名称"></el-table-column>
        <el-table-column prop="description" label="描述"></el-table-column>
        <el-table-column prop="functions" label="功能" width="300">
          <template v-slot="scope">
            <!-- 显示角色的功能列表，直接显示字符串 -->
            <span>{{ scope.row.functions }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" @click="editRole(scope.row)">编辑</el-button>
            <el-button type="text" @click="deleteRole(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div v-if="roles.length === 0" style="text-align: center; margin-top: 20px;">
        没有找到匹配的角色
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchName: '',
      roles: [] // 确保初始化为数组
    };
  },
  mounted() {
    this.fetchRoles();
  },
  methods: {
    fetchRoles() {
      // 向服务器请求角色数据
      this.axios.get('/api/role/get')
        .then(response => {
          // 假设 response.data 是 Result 对象
          const result = response.data;
          if (Array.isArray(result.data)) {
            this.roles = result.data; // 确保 roles 是数组
          } else {
            console.error('Error: Expected an array but got:', result.data);
          }
        })
        .catch(error => {
          console.error('Error fetching roles:', error);
        });
    },
    searchRole() {
    console.log('Searching role:', this.searchName); // 打印当前搜索的角色名称
      // 根据 searchName 搜索角色
    //  this.axios.get(`/api/role/search?name=${this.searchName}`)

        this.axios.get(`/api/role/search`, { params: { name:this.searchName} })
        .then(response => {
          // 假设 response.data 是 Result 对象
          const result = response.data;
          if (Array.isArray(result.data)) {
             console.log("Result data is an array:", result.data);
            this.roles = result.data; // 确保 roles 是数组
          } else {
            this.roles = []; // 如果没有匹配结果，清空 roles
            console.error('Error: Expected an array but got:', result.data);
          }
        })
        .catch(error => {
          this.roles = []; // 请求出错时也清空 roles
          console.error('Error searching roles:', error);
        });
    },

    editRole(role) {
      // 编辑角色逻辑
      console.log('编辑角色', role);
     this.$router.push(`/role/edit_role2/${role.name}`);
    },
    deleteRole(role) {
      // 删除角色逻辑
      console.log('删除角色', role);
      // 发送删除请求
      this.axios.delete(`/api/role/delete/${role.name}`)
        .then(() => {
          this.fetchRoles(); // 删除后重新获取角色列表
        })
        .catch(error => {
          console.error('Error deleting role:', error);
        });
    }
  }
}
</script>

<style scoped>
.box-card {
  margin: auto;
  width: 800px;
}
</style>
